//
//  RootViewController.m
//  sleepTest2
//
//  Created by apple on 2017/8/5.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "RootViewController.h"
#import "HomeViewController.h"
#import "NoteViewController.h"
#import "startSleepViewController.h"
#import "ShareViewController.h"
#import "MyViewController.h"
// 宏定义屏幕尺寸
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
// 设置tabBar高度，iPhone6之前的事49，6是96
CGFloat const tabViewHeight = 65;
// 设置标签栏按钮的宽度和高度
CGFloat const btnWidth = 73;
CGFloat const btnHeight = 55.89;

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 隐藏系统默认的样式
    self.tabBar.hidden = YES;
    
    // 调用初始化视图控制器
    [self initViewController];
    
    // 调用初始化标签栏
    [self initTabBarView];
}

// 初始化视图控制器
- (void)initViewController{
    // 初始化每个视图控制器
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    NoteViewController *noteVC = [[NoteViewController alloc] init];
    startSleepViewController *startSleepVC = [[startSleepViewController alloc] init];
    ShareViewController *shareVC = [[ShareViewController alloc] init];
    MyViewController *myVC = [[MyViewController alloc] init];
    
    // 数组用于存放视图控制器
    NSArray *vcArray = @[homeVC,noteVC,startSleepVC,shareVC,myVC];
    // 可变数组，用于存放标签控制器控制的导航控制器
    NSMutableArray *tabArray = [NSMutableArray arrayWithCapacity:vcArray.count];
    // 初始化导航控制器
    for (int i = 0; i < vcArray.count; i++) {
        UINavigationController *navCtrl = [[UINavigationController alloc]initWithRootViewController:vcArray[i]];
        [tabArray addObject:navCtrl];
    }
    // 将导航控制器给标签控制器
    self.viewControllers = tabArray;
    
}

// 自定义标签工具栏
- (void)initTabBarView{
    // 初始化标签工具栏视图
    _tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - tabViewHeight, kScreenWidth, tabViewHeight)];
    _tabBarView.backgroundColor = [UIColor colorWithRed:188/255.0 green:195/255.0 blue:232/255.0 alpha:1];
    [self.view addSubview:_tabBarView];
    // 新语法创建数组，拿到图片
    NSArray *imgArray = @[@"Home",@"Note",@"startSleep",@"Share",@"My"];
    // 创建按钮
    for (int i = 0; i < imgArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundImage:[UIImage imageNamed:imgArray[i]] forState:UIControlStateNormal];
        btn.frame = CGRectMake(btnWidth * i, (tabViewHeight - btnHeight)/2, btnWidth, btnHeight);
        // 为按钮设置tag，iOS中100以内的tag值有特殊用法，需跳过
        btn.tag = 100 + i;
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBarView addSubview:btn];
    }
}

// 标签栏按钮点击事件
- (void)btnAction:(UIButton *)button{
    // 根据tag值判断当前索引
    self.selectedIndex = button.tag - 100;
}

// 实现showTabBar:方法(是否显示工具栏）
- (void)showTabBar:(BOOL)show{
    CGRect frame = self.tabBarView.frame;
    if (show) {
        frame.origin.x = 0;
    }else{
        frame.origin.x = - kScreenWidth;
    }
    // 重新赋值frame
    [UIView animateWithDuration:0.2 animations:^{
        self.tabBarView.frame = frame;
    }completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
